package com.fzshang.shopping.wx.interceptor;

import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.service.XbxWxService;
import me.chanjar.weixin.common.api.WxConsts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Zhang on 2018/8/26.
 */
@Configuration
public class Appinterceptor extends BaseController implements HandlerInterceptor{

    @Autowired
    XbxWxService xbxWxService;
    @Value("${weixin.redirectURL}")
    String redirectURL = "";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();

        String stata = null;
        //解决 session失效后 重新跳转总是跳转某个默认地址
        //获取到当前请求的地址
        if (request.getQueryString() != null && !request.getQueryString().equals("")) {
            stata = request.getServletPath() + "?" + request.getQueryString();
        }else{
            stata=request.getServletPath();
        }
        //如果登陆信息失效  那么就重新登陆，并把本次访问地址当成stata参数传递  在用户授权后 跳转stata
        if (session.getAttribute("user") == null) {
            System.out.println("sessionWXUser为空:"+session.getAttribute("user"));
            //微信授权的地址
            String url = xbxWxService.getWxService().oauth2buildAuthorizationUrl(redirectURL, WxConsts.OAuth2Scope.SNSAPI_USERINFO,stata );
            redirect(request, response, url);
            return false;
        }
        System.out.println("sessionWXUser不为空:"+session.getAttribute("user"));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

    //对于请求是ajax请求重定向问题的处理方法
    public void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
        // 获取当前请求的路径
        //String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":80";
        System.out.println("basePath:" + basePath);
        // 如果request.getHeader("X-Requested-With") 返回的是"XMLHttpRequest"说明就是ajax请求，需要特殊处理 否则直接重定向就可以了
        if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
            //告诉ajax我是重定向
            response.setHeader("REDIRECT", "REDIRECT");
            // 告诉ajax我重定向的路径
            response.setHeader("CONTENTPATH", basePath + "/wx/user/login");
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        } else {
            response.sendRedirect(url);
        }

    }

}
